package com.autonavi.dextool.parse;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.autonavi.dextool.DataCatch;
import com.autonavi.dextool.MultidexSwitch;

import sun.tools.jconsole.inspector.XMBean;

public class ParseXml {

	public static final int TYPE_DEFAULT = 1;
	public static final int TYPE_ACTIVITY = 2;
	public static final int TYPE_APPLICATION = 3;
	public static final int TYPE_FRAGMENT = 4;
	public static final int TYPE_RECEIVER = 5;
	public static final int TYPE_SPECIAL = 6;
	
	private DataCatch mDataCatch;

	public ParseXml(String xmlPath){
		mDataCatch = DataCatch.getDataCatchIns();
		parseXML(xmlPath);
	}

	protected void parseXML(String path){

		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			File file = new File(path);
			MultidexSwitch.log(true,"  parse xml path :"+path);
			document = reader.read(file);
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}

		Element root = document.getRootElement();
		Iterator<?> it = root.elementIterator();

		while (it.hasNext()) {
			Element element = (Element) it.next();
			addItem(element);
		}
	}

	private void addItem(Element e){
		String key = e.attributeValue("class");
		switch (e.getName()) {
		case "impl":
		case "property":
			mDataCatch.xmlParseResult(new DefaultBean(TYPE_DEFAULT, key));
			break;
		case "special":
			mDataCatch.xmlParseResult(new DefaultBean(TYPE_SPECIAL, key));
			break;
		case "activity":
			mDataCatch.xmlParseResult(new ActivityBean(TYPE_ACTIVITY, key));
			break;
		case "application":
			mDataCatch.xmlParseResult(new ApplicationBean(TYPE_APPLICATION, key));
			break;
		case "receiver":
			mDataCatch.xmlParseResult(new ReceiverBean(TYPE_RECEIVER, key));
			break;
		case "fragment":
			mDataCatch.xmlParseResult(new FragmentBean(TYPE_FRAGMENT, key));
			break;
		default:
			break;
		}
	}

}
